package com.erp.order.purchasing.service.impl;

import com.erp.hr.base.service.impl.BaseServiceImpl;
import com.erp.hr.base.util.CommonUtils;
import com.erp.hr.base.vo.LegalityResultVO;
import com.erp.order.purchasing.dao.PurchasingRequisitionDao;
import com.erp.order.purchasing.dao.PurchasingRequisitionDetailDao;
import com.erp.order.purchasing.model.PurchaseOrderDetailVO;
import com.erp.order.purchasing.model.PurchasingRequisition;
import com.erp.order.purchasing.model.PurchasingRequisitionDetail;
import com.erp.order.purchasing.service.PurchasingRequisitionDetailService;
import com.erp.order.supply.model.SupplyMaterialDetailR;
import org.apache.commons.lang3.StringUtils;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 采购申请详情 业务实现层
 * @Author: syz
 * @blame syz
 * @CreateTime: 2021/4/6
 */
@Service
public class PurchasingRequisitionDetailServiceImpl extends BaseServiceImpl<PurchasingRequisitionDetailDao, PurchasingRequisitionDetail> implements PurchasingRequisitionDetailService {

    /**
     * 采购申请服务
     */
    @Lazy
    @Autowired
    private PurchasingRequisitionDao purchasingRequisitionDao;


    /**
     * 获取列表分页数据
     * @param s 数据验证sql
     * @param pages 分页对象
     * @param purchasingRequisitionDetail 查询条件对象
     * @return 列表分页数据
     */
    @Override
    public List<PurchasingRequisitionDetail> getDataObjectsForDataAuth(String s, Pages pages, PurchasingRequisitionDetail purchasingRequisitionDetail) {
        List<PurchasingRequisitionDetail> list = super.getDataObjectsForDataAuth(s, pages, purchasingRequisitionDetail);
        Set<Long> ids = new HashSet<>();
        for (PurchasingRequisitionDetail prd : list) {
            ids.add(prd.getRequisitionId());
        }
        List<PurchasingRequisition> prs = purchasingRequisitionDao.getListByIds(ids);
        for (PurchasingRequisitionDetail prd : list) {
            for (PurchasingRequisition pr : prs) {
                if (pr.getId().equals(prd.getRequisitionId())) {
                    prd.setCompanyName(pr.getCompanyName());
                    prd.setDepartmentName(pr.getDepartmentName());
                    break;
                }
            }
        }
        return list;
    }

    @Override
    public PurchasingRequisitionDetail addCompleteData(PurchasingRequisitionDetail purchasingRequisitionDetail) {
        return null;
    }

    /**
     * 为集合添加完整信息
     * @param list 需要添加完整信息的集合
     * @return 添加完整信息后的集合
     */
    @Override
    public List<PurchasingRequisitionDetail> addCompleteData(List<PurchasingRequisitionDetail> list) {
        for (PurchasingRequisitionDetail prd : list) {
            if (StringUtils.isNotEmpty(prd.getAccessory())) {
                prd.setAccessoryName(prd.getAccessory().split("FILE_NAME_START")[1]);
            }
        }
        return list;
    }

    @Override
    public LegalityResultVO isLegalityData(PurchasingRequisitionDetail purchasingRequisitionDetail) {
        return new LegalityResultVO(false, null);
    }

    @Override
    public boolean save(PurchasingRequisitionDetail purchasingRequisitionDetail) {
        return false;
    }

    /**
     * 更新对象
     * @param purchasingRequisitionDetail 需要更新的对象
     * @return 更新结果
     */
    @Override
    public boolean update(PurchasingRequisitionDetail purchasingRequisitionDetail) {
        dao.updateDataObject(purchasingRequisitionDetail);
        return true;
    }

    /**
     * 根据id删除对象及相关信息
     * @param id 需要删除对象的id
     * @return 删除结果
     */
    @Override
    public boolean linkDelete(Long id) {
        PurchasingRequisitionDetail prd = dao.getDataObject(id.toString());
        prd.setIsDel(CommonUtils.IS_DEL_YES);
        setUpdateInfo(prd);
        dao.updateDataObject(prd);
        return true;
    }

    /**
     * 根据申请id获取详情集合
     * @param id 申请id
     * @return 详情集合
     */
    @Override
    public List<PurchasingRequisitionDetail> getListByRequisitionId(Long id) {
        return dao.getListByRequisitionId(id);
    }

    /**
     * 获取单一采购订单详情
     * @param inquiryListId 询价单id
     * @return 采购订单详情
     */
    @Override
    public List<PurchaseOrderDetailVO> getOnlyOrderDetail(Long inquiryListId) {
        return dao.getOnlyOrderDetail(inquiryListId);
    }

    /**
     * 获取询比价采购订单详情
     * @param inquiryListId 询价单id
     * @return 采购订单详情
     */
    @Override
    public List<PurchaseOrderDetailVO> getOtherOrderDetail(Long inquiryListId) {
        return dao.getOtherOrderDetail(inquiryListId);
    }

    /**
     * 根据id集合获取对象集合
     * @param ids id集合
     * @return 对象集合
     */
    @Override
    public List<PurchasingRequisitionDetail> getByIds(Set<Long> ids) {
        return dao.getByIds(ids);
    }

    @Override
    public List<SupplyMaterialDetailR> getDetailsByOrderId(Long id) {
        return dao.getDetailsByOrderId(id);
    }

}